//
//  SeacherViewController.m
//  HealthA段
//
//  Created by dllo on 15/6/11.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SeacherViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "Base.h"
#import "MJRefresh.h"
#import "SearchTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "searchdetailViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface SeacherViewController ()
@property(nonatomic,retain)UISearchBar *MySearchBar;
@property(nonatomic, copy)NSString *keyword;
@property(nonatomic, retain)UITableView *table;
@property(nonatomic, retain)NSMutableArray *healArr;
@property(nonatomic,assign)NSInteger number;
@property(nonatomic, retain)UIView *tempview;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,retain)Base *health;
@end

@implementation SeacherViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.healArr = [NSMutableArray array];
        self.number = 1;
        self.page = 1;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"srne.jpg"]];
    self.MySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    [self.view addSubview:self.MySearchBar];
    self.MySearchBar.showsCancelButton = YES;
    self.MySearchBar.delegate = self;
    self.MySearchBar.placeholder = @"请输入关键字";
    
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, HEIGHT-104-49) style:UITableViewStylePlain];
    [self.view addSubview:self.table];
    self.table.rowHeight = 120;
    self.table.hidden = YES;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self PULLdata];
    
    self.tempview = [[UIView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT)];
    self.tempview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sorry"]];
    [self.view addSubview:self.tempview];
    [self.view bringSubviewToFront:self.tempview];
    self.tempview.hidden = YES;
}

- (void)getdata
{

    NSString *str = @"http://api.yi18.net/news/search?keyword=";
    NSString *keyStr = [NSString stringWithFormat:@"%@%@&page=%ld",str,self.MySearchBar.text,self.page];
    NSString *NewStr = [keyStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"%@",keyStr);
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:NewStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = dic[@"yi18"];
        for (NSMutableDictionary *temp in arr) {
            self.health = [[Base alloc] init];
            [_health setValuesForKeysWithDictionary:temp];
            [self.healArr addObject:_health];
            NSLog(@"%@",_health.title);
        }
        [self.table reloadData];
        [self.table.legendFooter endRefreshing];
        if (self.healArr.count == 0) {
            self.tempview.hidden = NO;
        }else{
            self.tempview.hidden = YES;
            [self.table reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"请求失败");
    }];
    self.page ++;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.healArr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[SearchTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.myweb loadHTMLString:[self.healArr[indexPath.row] title] baseURL:nil];
    NSString *str = [NSString stringWithFormat:@"http://www.yi18.net/%@",[self.healArr[indexPath.row] img]];
    NSURL *url = [NSURL URLWithString:str];
    [cell.searchImage sd_setImageWithURL:url];
    return cell;
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    self.table.hidden = NO;
    if (self.number == 1) {
        [self getdata];
        self.number ++;
    }else{
        [self.healArr removeAllObjects];
        [self getdata];
    }
}

- (void)creatResultView
{
    self.tempview = [[UIView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT)];
    self.tempview.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.tempview];
    UILabel *laber = [[UILabel alloc] initWithFrame:CGRectMake(40, 100, 300, 40)];
    laber.text = [NSString stringWithFormat:@"没有搜索到有关'%@'的相关内容",self.MySearchBar.text];
    [self.tempview addSubview:laber];
    [self.view bringSubviewToFront:self.tempview];
}

- (void)PULLdata
{
     [self.table addLegendFooterWithRefreshingBlock:^{
         [self getdata];
         [self.table reloadData];
         [self.table.footer endRefreshing];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    searchdetailViewController *seVC = [[searchdetailViewController alloc] init];
    seVC.myid = [self.healArr[indexPath.row]id];
    seVC.mytype = [self.healArr[indexPath.row]type];
    [self.navigationController pushViewController:seVC animated:YES];
}


-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
